<?php

namespace App\Http\Requests;

use App\Http\Requests\Request;

class InstitutionRequest extends Request
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $createRules = [
            'subject'     => 'required',
            'type'        => 'required|in:law,invest,makerspace,finance',
            'zone_id'     => 'required|numeric',
            'address'     => 'sometimes',
            'description' => 'required',
            'sort'        => 'required|numeric',
            'tel'         => 'sometimes',
            'represent'   => 'required',
            'email'       => 'sometimes|e_mail',
            'url'         => 'sometimes|url',
            'fax'         => 'sometimes',
            'icon'        => 'sometimes',
            'title'       => 'sometimes',
            'img'         => 'sometimes',
            'detail_img'  => 'sometimes',
            'license'     => 'sometimes',
            'remark'      => 'sometimes',
            'stick'       => 'required|in:0,1',
            'recommend'   => 'required|in:0,1',
        ];

        $editRules = [
            'id'          => 'required|numeric',
            'subject'     => 'required',
            'type'        => 'required|in:law,invest,makerspace,finance',
            'zone_id'     => 'required|numeric',
            'address'     => 'sometimes',
            'description' => 'required',
            'sort'        => 'required|numeric',
            'tel'         => 'sometimes',
            'represent'   => 'required',
            'email'       => 'sometimes|e_mail',
            'url'         => 'sometimes|url',
            'fax'         => 'sometimes',
            'icon'        => 'sometimes',
            'title'       => 'sometimes',
            'img'         => 'sometimes',
            'detail_img'  => 'sometimes',
            'license'     => 'sometimes',
            'remark'      => 'sometimes',
            'stick'       => 'required|in:0,1',
            'recommend'   => 'required|in:0,1',
            'status'      => 'required|in:-1,1',
        ];
//        return array();

        $data = Request::all();
        if ($data['formType'] == 'create') {
            return $createRules;
        } elseif ($data['formType'] == 'edit') {
            return $editRules;
        }
    }

    public function attributes()
    {
        $attributes = array(
            'subject'     => '机构名称',
            'type'        => '机构类型',
            'zone_id'     => '所在地区',
            'address'     => '详细地址',
            'description' => '机构描述',
            'sort'        => '排序',
            'tel'         => '机构电话',
            'represent'   => '法人代表',
            'email'       => '机构邮箱 ',
            'url'         => '机构链接',
            'fax'         => '机构传真',
            'icon'        => '机构logo',
            'title'       => '机构简介',
            'img'         => '简介下面的大图',
            'detail_img'  => '详细页的背景图',
            'license'     => '营业执照img',
            'remark'      => '营业执照备注',
            'stick'       => '是否置顶',
            'recommend'   => '是否推荐',
        );

        return $attributes;
    }

    public function messages()
    {
        $messages = [
            'required' => ':attribute为必填选项',
            'numeric'  => ':attribute必须为数字',
            'type.in'  => ':attribute必须为法律机构、投资机构、创客空间机构、经济机构中的一种',
            'status.in'  => ':attribute必须为审核通过或审核不通过的一种',
            'e_mail'   => ':attribute必须为合乎规范的邮箱格式',
            'url'      => ':attribute必须为合乎规范的链接',
        ];

        return $messages;
    }
}
